package com.love.testnetwork;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.love.testnetwork.entity.BaseResponse;
import com.love.testnetwork.entity.GuestureEntity;
import com.love.testnetwork.entity.UserEntity;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";


    private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择

    private TextView tv_title;
    private ImageView image;


    private static final String UPDATE_URL = "http://app1.ichezheng.com/CarcareService/setting/setImageFromAndroid";

    private Map<String, String> map = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        map.put("userid", "58064");
        map.put("session", "64510d0f53a4465d9099ced1829eb18f");
        map.put("imei", "869267012409343");

        tv_title = (TextView) findViewById(R.id.tv_title);
        image = (ImageView) findViewById(R.id.image);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PHOTO_REQUEST_GALLERY) {
            // 从相册返回的数据
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                if (uri != null) {

                    String realPathFromUri = ContentUtil.getPath(MainApp.getAppContext(), uri);
                    tv_title.setText(realPathFromUri);
                    image.setImageURI(uri);

                    update(realPathFromUri);
                }
            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 上传头像
     *
     * @param path 图片路径
     */
    private void update(String path) {

        File file1 = new File(path);

        RequestBody requestBody1 = RequestBody.create(MediaType.parse("multipart/form-data"), file1);


        Call<ResponseBody> responseBodyCall = HttpMethods.getInstance().updateImage(UPDATE_URL, map, "user_icon.jpg", requestBody1);
        responseBodyCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.i(TAG, "onResponse: " + response);
                Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    /**
     * 使用第二种方式获取Apiservice
     * 区别是请求的时候 业务逻辑中 多写了一句代码
     * 使用接口方式 ，代码没有在业务逻辑中
     *
     * @param view
     */
    public void testTwo(View view) {
        ApiService apiService = HttpMethods.getInstance().createApi(ApiService.class);

        Call<BaseResponse<UserEntity>> userInfo = apiService.getUserInfo("58064", "64510d0f53a4465d9099ced1829eb18f");
        userInfo.enqueue(new Callback<BaseResponse<UserEntity>>() {
            @Override
            public void onResponse(Call<BaseResponse<UserEntity>> call, Response<BaseResponse<UserEntity>> response) {
                Log.i(TAG, "onResponse: " + response.body().getInfolist().getUSER_NAME());
                Toast.makeText(MainActivity.this, "使用第二种方式获取成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<BaseResponse<UserEntity>> call, Throwable t) {

            }
        });
    }

    /**
     * 使用rxjava
     *
     * @param view
     */
    public void testRxJava(View view) {
        HttpUtils.test();
    }

    /**
     * 上传图片
     *
     * @param view
     */
    public void onUploade(View view) {
        gallery();
    }

    public void gallery() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
    }

    /**
     * 获取密码
     *
     * @param view
     */
    public void getUserPwdData(View view) {
        Call<GuestureEntity> guestureInfo = HttpMethods.getInstance().getGuestureInfo(map);

        guestureInfo.enqueue(new Callback<GuestureEntity>() {
            @Override
            public void onResponse(Call<GuestureEntity> call, Response<GuestureEntity> response) {
                Toast.makeText(MainActivity.this, response.body().getValue(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<GuestureEntity> call, Throwable t) {

            }
        });
    }

    /**
     * 获取用户信息
     *
     * @param view
     */
    public void getUserInfoData(View view) {
        Log.i(TAG, "load: 开始");

        Call<BaseResponse<UserEntity>> userInfo = HttpMethods.getInstance().getUserInfo("58064", "64510d0f53a4465d9099ced1829eb18f");

        userInfo.enqueue(new Callback<BaseResponse<UserEntity>>() {

            @Override
            public void onResponse(Call<BaseResponse<UserEntity>> call, Response<BaseResponse<UserEntity>> response) {
                Log.i(TAG, "onResponse: 结束");
                tv_title.setText(response.body().getInfolist().getUSER_NAME());

                Toast.makeText(MainActivity.this, "用户名发生变化", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<BaseResponse<UserEntity>> call, Throwable t) {

            }
        });
    }
}
